package com.example.mymvvmdemo.models.bind_device;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadActivity;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.ActivityBindDeviceBinding;
import com.example.mymvvmdemo.dialog.LoadingAnimDialog;
import com.example.mymvvmdemo.net.presenter.PostBodyPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.HashMap;

public class BindDeviceActivity extends BaseLoadActivity<ActivityBindDeviceBinding> implements View.OnClickListener, IView.PostBodyView {

    public static void startBindDeviceActivity(Activity activity, String name, String id, String mac, String code, String type) {
        Intent intent = new Intent(activity, BindDeviceActivity.class);
        intent.putExtra("name", name);
        intent.putExtra("id", id);
        intent.putExtra("mac", mac);
        intent.putExtra("code", code);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }


    private BindDeviceViewModel viewModel;
    private PostBodyPresenter postBodyPresenter;

    /*** 加载时候的Dialog */
    private LoadingAnimDialog loadingDialog;

    @Override
    protected void showDialog() {
        if (loadingDialog == null)
            loadingDialog = new LoadingAnimDialog(this, getResources().getString(R.string.load));
        if (!loadingDialog.isShowing()) loadingDialog.show(this);
    }

    @Override
    protected void dismissDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.close(this);
        }
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(this).get(BindDeviceViewModel.class);
        postBodyPresenter = new PostBodyPresenter(this);
        myGetIntent();
        initTitle(getString(R.string.add_device_bind_title));
        uiHandler.sendEmptyMessageDelayed(viewModel.HANDLER_REGISTER_SUCCESS, viewModel.HANDLER_BIND_DEVICE);
    }

    private void startRegisterDevice() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name", viewModel.name);
        hashMap.put("device_id", viewModel.id);
        hashMap.put("device_type", viewModel.type);
        hashMap.put("verify_code", viewModel.code);
        hashMap.put("mac_address", viewModel.mac);
        postBodyPresenter.startLoadPostBody(UrlConstants.registerWithCode, hashMap, UrlTypeConstants.registerWithCode);
    }

    @Override
    protected void initListener() {
        binding.include.ivBack.setOnClickListener(this);
        binding.tvNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_next:
                viewModel.startMainActivity(this);
                break;
            default:
                break;
        }
    }

    /***********************************Handler**************************************/
    @SuppressLint("HandlerLeak")
    private Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == viewModel.HANDLER_REGISTER_ING) {
                registerIngView();
            }
            if (msg.what == viewModel.HANDLER_REGISTER_SUCCESS) {
                registerSuccessView();
                startRegisterDevice();
            }
            if (msg.what == viewModel.HANDLER_BIND_DEVICE_ING) {
                bindDeviceIngView();
            }
            if (msg.what == viewModel.HANDLER_BIND_DEVICE_SUCCESS) {
                bindDeviceSuccessView();
            }
        }
    };


    /***********************************自定义方法**************************************/
    public void registerIngView() {
        binding.progressRegister.setVisibility(View.VISIBLE);
        binding.ivRegisterSuccess.setVisibility(View.GONE);
        binding.tvRegister.setText(R.string.add_device_registering);
    }

    public void registerSuccessView() {
        binding.progressRegister.setVisibility(View.INVISIBLE);
        binding.ivRegisterSuccess.setVisibility(View.VISIBLE);
        binding.tvRegister.setText(R.string.add_device_registering_success);
    }

    public void bindDeviceIngView() {
        binding.progressBind.setVisibility(View.VISIBLE);
        binding.ivBindSuccess.setVisibility(View.GONE);
        binding.tvBind.setText(R.string.add_device_binding);
    }

    public void bindDeviceSuccessView() {
        binding.progressBind.setVisibility(View.INVISIBLE);
        binding.ivBindSuccess.setVisibility(View.VISIBLE);
        binding.tvBind.setText(R.string.add_device_binding_success);
    }

    public void initTitle(String title) {
        binding.include.tvTitle.setVisibility(View.VISIBLE);
        binding.include.tvTitle.setText(title);
    }

    private void myGetIntent() {
        viewModel.name = getIntent().getStringExtra("name");
        viewModel.id = getIntent().getStringExtra("id");
        viewModel.mac = getIntent().getStringExtra("mac");
        viewModel.code = getIntent().getStringExtra("code");
        viewModel.type = getIntent().getStringExtra("type");
        LogUtils.d("name:" + viewModel.name
                + ",id:" + viewModel.id
                + ",mac:" + viewModel.mac
                + ",code:" + viewModel.code
                + ",type:" + viewModel.type);
    }

    /***********************************数据请求**************************************/
    @Override
    public void loadPostBodySuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.registerWithCode)) {
            loadRegisterWithCodeSuccess(result, code);
        }
    }

    @Override
    public void loadPostBodyFail(String message, String type) {
        if (type.equals(UrlTypeConstants.registerWithCode)) {
            loadRegisterWithCodeFail();
        }
    }

    @Override
    public void loadPostBodyAccountError(int code) {

    }

    @Override
    public void loadPostBodyNetWorkError(String type) {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }

    /***********************************处理请求后数据**************************************/
    private void loadRegisterWithCodeSuccess(String result, int code) {
        if (code != 200) {
            loadRegisterWithCodeFail();
            return;
        }
        dismissDialog();
        toast(R.string.add_device_binding_success, ToastUtils.TOA_S);
        uiHandler.sendEmptyMessageDelayed(viewModel.HANDLER_BIND_DEVICE_SUCCESS, viewModel.HANDLER_BIND_DEVICE);
    }

    private void loadRegisterWithCodeFail() {
        dismissDialog();
        toast(R.string.add_device_binding_fail, ToastUtils.TOA_F);
    }

}